﻿using Google.Protobuf.WellKnownTypes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace Textbook
{
    public partial class IssForm : Form
    {
        public IssForm(Home home, Issue issue)
        {
            InitializeComponent();
            this.home = home;
            this.issue = issue;
            getBill();
        }

        Home home;

        Issue issue;

        SQL sql = new SQL();

        private void getBill()
        {
            DataSet ds = sql.selectBill(0);
            if (ds.Tables[0].Rows.Count > 0)
            {
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
                if (e1)
                {
                    DataGridViewButtonColumn column = new DataGridViewButtonColumn();
                    column.Text = "发 书";
                    column.Name = "编 辑";
                    column.UseColumnTextForButtonValue = true;
                    dataGridView1.Columns.Add(column);
                    e1 = false;
                }
            }
        }

        bool e1 = true,e2=true;
        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPageIndex == 0)
            {
                //购书单
                getBill();

            }
            else if (e.TabPageIndex == 1)
            {
                //缺书单
                DataSet ds = sql.selectLess(1);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView2.DataSource = ds.Tables[0].DefaultView;
                    if (e2)
                    {
                        DataGridViewButtonColumn column = new DataGridViewButtonColumn();
                        column.Text = "删 除";
                        column.Name = "编 辑";
                        column.UseColumnTextForButtonValue = true;
                        dataGridView2.Columns.Add(column);
                        e2 = false;
                    }
                }
            }
            else if (e.TabPageIndex == 2)
            {
                DataSet ds = sql.selectBook();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView3.DataSource = ds.Tables[0].DefaultView;
                }
            }
            else if (e.TabPageIndex == 3)
            {
                DataSet ds = sql.selectWarehouse();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView4.DataSource = ds.Tables[0].DefaultView;
                }
            }
            else
            {
                if (MessageBox.Show("确定要退出...", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    home.openForm(new Login(home));
                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 &&
                MessageBox.Show("确定发书？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                //领书
                string b_id = dataGridView1.Rows[e.RowIndex].Cells["订单号"].Value.ToString();
                int book_id = int.Parse(dataGridView1.Rows[e.RowIndex].Cells["书籍编号"].Value.ToString());
                if (sql.updateBillState(b_id, book_id, 1))
                {
                    MessageBox.Show("书籍发放成功...");
                    dataGridView1.Rows.RemoveAt(e.RowIndex);
                }
                else
                {
                    MessageBox.Show("发放失败...");
                }
            }
        }

        //登记缺书
        private void regist_Click(object sender, EventArgs e)
        {
            LessForm lessForm = new LessForm();
            lessForm.ShowDialog();
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 &&
                MessageBox.Show("确定删除？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string l_id = dataGridView2.Rows[e.RowIndex].Cells["缺书单号"].Value.ToString();
                int book_id = int.Parse(dataGridView2.Rows[e.RowIndex].Cells["书籍编号"].Value.ToString());
                if (sql.updateLessState(l_id, book_id, -1))
                {
                    MessageBox.Show("删除成功...");
                    dataGridView2.Rows.RemoveAt(e.RowIndex);
                }
                else
                {
                    MessageBox.Show("删除失败...");
                }
            }
        }
    }
}
